package cn.bud.msgbus.server.web.dtq;

import com.zl9c.general.utils.SslUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * SubscribeHttpDTQ
 *
 * @author hxj
 * @version 1.0
 * @description
 * @date 2024-07-15 13:25
 */
@Data
public class SubscribeHttpDTQ {
    @ApiModelProperty(value = "回调URL", required = true)
    @NotBlank(message = "回调地址必填")
    @Pattern(regexp = "^(https?://)([a-zA-Z0-9.-]+)(:[0-9]+)?(/\\S*)?$")
    private String callbackUrl;
    @ApiModelProperty(value = "订阅的消息话题")
    private Set<String> topics;

    @ApiModelProperty(value = "推送开始时间，为空则立即推送")
    private LocalDateTime startTime;

    @ApiModelProperty(value = "推送截止时间，为空则一直推送")
    private LocalDateTime endTime;

    public static boolean validUrl(String url) {
        try {
            URL url1 = new URL(url);
            String pro = url1.getProtocol();
            if (pro.startsWith("https://")) {
                SslUtils.ignoreSsl();
            }
            URLConnection connection = url1.openConnection();
            connection.setConnectTimeout(1000);
            connection.connect();
        }catch (Exception e) {
            return false;
        }
        return true;
    }
}
